@@ -10,16 +10,16 @@ import android.net.ConnectivityManager; |
||
10 | 10 |
import android.os.IBinder; |
11 | 11 |
import android.support.annotation.StringRes; |
12 | 12 |
|
13 |
+import com.android.common.utils.LogHelper; |
|
13 | 14 |
import com.android.common.utils.NetworkUtil; |
15 |
+import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; |
|
14 | 16 |
|
15 | 17 |
import java.text.SimpleDateFormat; |
16 | 18 |
import java.util.ArrayList; |
17 | 19 |
import java.util.Date; |
18 | 20 |
|
19 | 21 |
import ai.pai.lensman.App; |
20 |
-import ai.pai.lensman.BuildConfig; |
|
21 | 22 |
import ai.pai.lensman.R; |
22 |
-import ai.pai.lensman.base.BaseInteractor; |
|
23 | 23 |
import ai.pai.lensman.bean.PhotoBean; |
24 | 24 |
import ai.pai.lensman.bean.SessionBean; |
25 | 25 |
import ai.pai.lensman.db.DBService; |
@@ -27,25 +27,24 @@ import ai.pai.lensman.db.Preferences; |
||
27 | 27 |
import ai.pai.lensman.service.OrderDealService; |
28 | 28 |
import ai.pai.lensman.service.UploadService; |
29 | 29 |
|
30 |
-class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorListener<ArrayList<String>>,SyncTimeInteractor.SyncTimeListener, |
|
30 |
+class MainPresenter implements MainContract.Presenter,SyncTimeInteractor.SyncTimeListener, |
|
31 | 31 |
UploadService.PhotoUploadListener, QueryBoxStatusInteractor.BoxStatusListener,OrderDealService.OrderServiceListener { |
32 | 32 |
|
33 | 33 |
private int sessionSeq; |
34 | 34 |
private String lensmanId; |
35 |
- private ArrayList<String> sessionIds; |
|
36 | 35 |
|
37 | 36 |
private ArrayList<SessionBean> sessionList; |
38 | 37 |
|
39 | 38 |
private MainContract.View view; |
40 |
- private FetchSessionIdsInteractor fetchSessionIdsInteractor; |
|
41 |
- private QueryBoxStatusInteractor boxStatusInteractor; |
|
39 |
+ |
|
42 | 40 |
private SyncTimeInteractor syncTimeInteractor; |
41 |
+ private QueryBoxStatusInteractor boxStatusInteractor; |
|
43 | 42 |
|
44 | 43 |
private ServiceConnection uploadServiceConnection; |
45 | 44 |
private ServiceConnection orderServiceConnection; |
46 | 45 |
|
47 |
- private boolean isBoxConnected = true; |
|
48 | 46 |
private boolean isTimeSync = false; |
47 |
+ private boolean isBoxConnected = true; |
|
49 | 48 |
|
50 | 49 |
|
51 | 50 |
private BroadcastReceiver wifiReceiver = new BroadcastReceiver() { |
@@ -65,8 +64,7 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL |
||
65 | 64 |
MainPresenter(MainContract.View view){ |
66 | 65 |
this.view = view; |
67 | 66 |
this.lensmanId = Preferences.getInstance().getLensManId(); |
68 |
- sessionIds = new ArrayList<>(); |
|
69 |
- fetchSessionIdsInteractor = new FetchSessionIdsInteractor(lensmanId,100,this); |
|
67 |
+ |
|
70 | 68 |
boxStatusInteractor = new QueryBoxStatusInteractor(this); |
71 | 69 |
syncTimeInteractor = new SyncTimeInteractor(lensmanId,this); |
72 | 70 |
uploadServiceConnection = new ServiceConnection() { |
@@ -104,20 +102,11 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL |
||
104 | 102 |
} |
105 | 103 |
view.setNewSessionBtnEnabled(false); |
106 | 104 |
view.showTimeSyncView(); |
107 |
- if(sessionIds==null|| sessionIds.size()<20){ |
|
108 |
- fetchSessionIdsInteractor.startJob(); |
|
109 |
- } |
|
110 | 105 |
if(!isTimeSync){ |
111 | 106 |
syncTimeInteractor.startJob(); |
112 | 107 |
} |
113 | 108 |
boxStatusInteractor.startJob(); |
114 | 109 |
|
115 |
- if(BuildConfig.isTestMode){ |
|
116 |
- if(sessionIds==null || sessionIds.size()==0){ |
|
117 |
- sessionIds = new ArrayList<>(); |
|
118 |
- sessionIds.add(String.valueOf(System.currentTimeMillis())); |
|
119 |
- } |
|
120 |
- } |
|
121 | 110 |
registerWifiChangeReceiver(); |
122 | 111 |
App.getAppContext().startService(new Intent(App.getAppContext(), OrderDealService.class)); |
123 | 112 |
App.getAppContext().bindService(new Intent(App.getAppContext(),OrderDealService.class), |
@@ -134,10 +123,6 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL |
||
134 | 123 |
if(!isTimeSync){ |
135 | 124 |
return; |
136 | 125 |
} |
137 |
- if(sessionIds==null|sessionIds.size()<20){ |
|
138 |
- return; |
|
139 |
- } |
|
140 |
- |
|
141 | 126 |
view.setNewSessionBtnEnabled(true); |
142 | 127 |
if(sessionList.size()==0){ |
143 | 128 |
view.showEmptyView(); |
@@ -152,7 +137,6 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL |
||
152 | 137 |
public void stop() { |
153 | 138 |
boxStatusInteractor.cancelJob(); |
154 | 139 |
syncTimeInteractor.cancelJob(); |
155 |
- fetchSessionIdsInteractor.cancelJob(); |
|
156 | 140 |
App.getAppContext().unbindService(uploadServiceConnection); |
157 | 141 |
App.getAppContext().unbindService(orderServiceConnection); |
158 | 142 |
App.getAppContext().unregisterReceiver(wifiReceiver); |
@@ -165,9 +149,9 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL |
||
165 | 149 |
sessionBean.lensmanId = lensmanId; |
166 | 150 |
sessionBean.sessionDate=getSessionDateInLongFormat(); |
167 | 151 |
sessionBean.sessionSeq = sessionSeq+1; |
168 |
- sessionBean.sessionId =sessionIds.get(0); |
|
152 |
+ sessionBean.sessionId = lensmanId+ new Md5FileNameGenerator().generate(String.valueOf(System.currentTimeMillis())); |
|
153 |
+ LogHelper.d("czy","createNewSession = "+sessionBean.sessionId); |
|
169 | 154 |
sessionList.add(sessionBean); |
170 |
- sessionIds.remove(0); |
|
171 | 155 |
return sessionBean; |
172 | 156 |
} |
173 | 157 |
|
@@ -185,22 +169,6 @@ class MainPresenter implements MainContract.Presenter,BaseInteractor.InteractorL |
||
185 | 169 |
return Long.parseLong(dateStr); |
186 | 170 |
} |
187 | 171 |
|
188 |
- @Override |
|
189 |
- public void onInteractSuccess(ArrayList<String> sessionIds) { |
|
190 |
- this.sessionIds.addAll(sessionIds); |
|
191 |
- onDataReady(); |
|
192 |
- } |
|
193 |
- |
|
194 |
- @Override |
|
195 |
- public void onInteractFail(String errorMsg) { |
|
196 |
- if(NetworkUtil.isWifiConnected(App.getAppContext())){ |
|
197 |
- fetchSessionIdsInteractor.startJob(); |
|
198 |
- }else{ |
|
199 |
- view.showBoxDisconnectedView(); |
|
200 |
- } |
|
201 |
- |
|
202 |
- } |
|
203 |
- |
|
204 | 172 |
|
205 | 173 |
@Override |
206 | 174 |
public synchronized void onPhotoUploaded(PhotoBean bean) { |
@@ -10,8 +10,9 @@ import com.android.common.utils.LogHelper; |
||
10 | 10 |
|
11 | 11 |
import org.json.JSONObject; |
12 | 12 |
|
13 |
+import java.text.SimpleDateFormat; |
|
14 |
+import java.util.Date; |
|
13 | 15 |
import java.util.HashMap; |
14 |
-import java.util.Random; |
|
15 | 16 |
|
16 | 17 |
import ai.pai.lensman.BuildConfig; |
17 | 18 |
import ai.pai.lensman.R; |
@@ -30,7 +31,7 @@ public class SyncTimeInteractor implements BaseInteractor { |
||
30 | 31 |
private SyncTimeListener listener; |
31 | 32 |
private boolean isCancelled; |
32 | 33 |
|
33 |
- public SyncTimeInteractor(String lensmanId, SyncTimeListener listener){ |
|
34 |
+ public SyncTimeInteractor(String lensmanId, SyncTimeListener listener) { |
|
34 | 35 |
this.listener = listener; |
35 | 36 |
this.lensmanId = lensmanId; |
36 | 37 |
} |
@@ -39,61 +40,60 @@ public class SyncTimeInteractor implements BaseInteractor { |
||
39 | 40 |
public void startJob() { |
40 | 41 |
cancelJob(); |
41 | 42 |
isCancelled = false; |
42 |
- HashMap<String,String> params = new HashMap<>(); |
|
43 |
- params.put("user_id",lensmanId); |
|
44 |
- if(BuildConfig.isTestMode){ |
|
45 |
- if(new Random().nextInt(100)%2==0){ |
|
46 |
- listener.onTimeSync(true,0); |
|
47 |
- }else{ |
|
48 |
- listener.onTimeSync(false,R.string.sync_time_box_error); |
|
49 |
- } |
|
50 |
- } |
|
51 |
- LogHelper.d("czy","开始时间同步任务"); |
|
52 |
- timeSyncTask = new HttpPostTask(params){ |
|
43 |
+ HashMap<String, String> params = new HashMap<>(); |
|
44 |
+ params.put("user_id", lensmanId); |
|
45 |
+ LogHelper.d("czy", "开始时间同步任务"); |
|
46 |
+ timeSyncTask = new HttpPostTask(params) { |
|
53 | 47 |
|
54 | 48 |
int msgId = R.string.sync_time_box_error; |
55 | 49 |
|
56 | 50 |
@Override |
57 | 51 |
protected boolean parseResponse(String response) { |
58 |
- LogHelper.d("czy","时间同步任务 服务器应答="+response); |
|
59 |
- try{ |
|
52 |
+ LogHelper.d("czy", "时间同步任务 服务器应答=" + response); |
|
53 |
+ String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); |
|
54 |
+ try { |
|
60 | 55 |
JSONObject json = new JSONObject(response); |
61 | 56 |
int status = json.getInt("status"); |
62 |
- if(status == 200){ |
|
57 |
+ if (status == 200) { |
|
63 | 58 |
JSONObject data = json.getJSONObject("data"); |
64 |
- String time = data.getString("time"); |
|
65 |
- HashMap<String,String> boxParams = new HashMap<>(); |
|
66 |
- boxParams.put("time",time); |
|
67 |
- if(isCancelled){ |
|
68 |
- return false; |
|
69 |
- } |
|
70 |
- String setTimeResp = HttpUtils.doHttpPost(BoxUrlContainer.SET_TIME_URL,boxParams); |
|
71 |
- LogHelper.d("czy","时间同步任务 box设置时间应答="+setTimeResp); |
|
72 |
- if(isCancelled){ |
|
73 |
- return false; |
|
74 |
- } |
|
75 |
- SystemClock.sleep(1000); |
|
76 |
- if(isCancelled){ |
|
77 |
- return false; |
|
78 |
- } |
|
79 |
- String boxResp = HttpUtils.doHttpPost(BoxUrlContainer.BOX_TIME_URL,null); |
|
80 |
- LogHelper.d("czy","时间同步任务 box获取时间应答="+boxResp); |
|
81 |
- if(isCancelled){ |
|
82 |
- return false; |
|
83 |
- } |
|
84 |
- JSONObject boxJson = new JSONObject(boxResp); |
|
85 |
- int boxStatus = boxJson.getInt("status"); |
|
86 |
- if(boxStatus == 200){ |
|
87 |
- String boxTime = boxJson.getJSONObject("data").getString("time"); |
|
88 |
- if(time.substring(0,13).equals(boxTime.substring(0,13))){ |
|
89 |
- return true; |
|
90 |
- } |
|
59 |
+ time = data.getString("time"); |
|
60 |
+ } |
|
61 |
+ } catch (Exception e) { |
|
62 |
+ LogHelper.d("czy", "时间同步任务 服务器应答异常,采用本地时间 " + time); |
|
63 |
+ } |
|
64 |
+ try { |
|
65 |
+ HashMap<String, String> boxParams = new HashMap<>(); |
|
66 |
+ boxParams.put("time", time); |
|
67 |
+ if (isCancelled) { |
|
68 |
+ return false; |
|
69 |
+ } |
|
70 |
+ String setTimeResp = HttpUtils.doHttpPost(BoxUrlContainer.SET_TIME_URL, boxParams); |
|
71 |
+ LogHelper.d("czy", "时间同步任务 box设置时间应答=" + setTimeResp); |
|
72 |
+ if (isCancelled) { |
|
73 |
+ return false; |
|
74 |
+ } |
|
75 |
+ SystemClock.sleep(1000); |
|
76 |
+ if (isCancelled) { |
|
77 |
+ return false; |
|
78 |
+ } |
|
79 |
+ String boxResp = HttpUtils.doHttpPost(BoxUrlContainer.BOX_TIME_URL, null); |
|
80 |
+ LogHelper.d("czy", "时间同步任务 box获取时间应答=" + boxResp); |
|
81 |
+ if (isCancelled) { |
|
82 |
+ return false; |
|
83 |
+ } |
|
84 |
+ if(BuildConfig.isTestMode){ |
|
85 |
+ return true; |
|
86 |
+ } |
|
87 |
+ JSONObject boxJson = new JSONObject(boxResp); |
|
88 |
+ int boxStatus = boxJson.getInt("status"); |
|
89 |
+ if (boxStatus == 200) { |
|
90 |
+ String boxTime = boxJson.getJSONObject("data").getString("time"); |
|
91 |
+ if (time.substring(0, 13).equals(boxTime.substring(0, 13))) { |
|
92 |
+ return true; |
|
91 | 93 |
} |
92 |
- }else{ |
|
93 |
- msgId = R.string.sync_time_server_error; |
|
94 | 94 |
} |
95 |
- }catch (Exception e){ |
|
96 |
- LogHelper.e("czy","时间同步任务发生异常="+e); |
|
95 |
+ } catch (Exception e) { |
|
96 |
+ LogHelper.e("czy", "时间同步任务发生异常=" + e); |
|
97 | 97 |
} |
98 | 98 |
return false; |
99 | 99 |
} |
@@ -101,15 +101,15 @@ public class SyncTimeInteractor implements BaseInteractor { |
||
101 | 101 |
@Override |
102 | 102 |
protected void onPostFail() { |
103 | 103 |
super.onPostFail(); |
104 |
- listener.onTimeSync(false,msgId); |
|
105 |
- LogHelper.d("czy","时间同步失败"); |
|
104 |
+ listener.onTimeSync(false, msgId); |
|
105 |
+ LogHelper.d("czy", "时间同步失败"); |
|
106 | 106 |
} |
107 | 107 |
|
108 | 108 |
@Override |
109 | 109 |
protected void onPostSuccess() { |
110 | 110 |
super.onPostSuccess(); |
111 |
- listener.onTimeSync(true,0); |
|
112 |
- LogHelper.d("czy","时间同步成功"); |
|
111 |
+ listener.onTimeSync(true, 0); |
|
112 |
+ LogHelper.d("czy", "时间同步成功"); |
|
113 | 113 |
} |
114 | 114 |
}; |
115 | 115 |
timeSyncTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.SERVER_TIME_URL); |
@@ -117,18 +117,18 @@ public class SyncTimeInteractor implements BaseInteractor { |
||
117 | 117 |
|
118 | 118 |
@Override |
119 | 119 |
public void cancelJob() { |
120 |
- if(timeSyncTask==null){ |
|
120 |
+ if (timeSyncTask == null) { |
|
121 | 121 |
return; |
122 | 122 |
} |
123 |
- if(timeSyncTask.getStatus()== AsyncTask.Status.RUNNING){ |
|
123 |
+ if (timeSyncTask.getStatus() == AsyncTask.Status.RUNNING) { |
|
124 | 124 |
timeSyncTask.cancel(true); |
125 | 125 |
} |
126 | 126 |
isCancelled = true; |
127 | 127 |
timeSyncTask = null; |
128 | 128 |
} |
129 | 129 |
|
130 |
- public interface SyncTimeListener{ |
|
130 |
+ public interface SyncTimeListener { |
|
131 | 131 |
|
132 |
- void onTimeSync(boolean result,@StringRes int strId); |
|
132 |
+ void onTimeSync(boolean result, @StringRes int strId); |
|
133 | 133 |
} |
134 | 134 |
} |
@@ -29,7 +29,7 @@ public class HttpPostTask extends AsyncTask<String,Integer,Boolean> { |
||
29 | 29 |
rootUrl+="?platform=android"; |
30 | 30 |
} |
31 | 31 |
rootUrl+="&version="+ BuildConfig.VERSION_NAME; |
32 |
- rootUrl+="&channel="+"guanwang"; |
|
32 |
+ rootUrl+="&channel="+BuildConfig.FLAVOR; |
|
33 | 33 |
String response= HttpUtils.doHttpPost(rootUrl,httpParams); |
34 | 34 |
return parseResponse(response); |
35 | 35 |
} |
@@ -16,7 +16,7 @@ import java.util.Set; |
||
16 | 16 |
public class HttpUtils { |
17 | 17 |
|
18 | 18 |
private static final int READ_TIME_OUT = 20000; |
19 |
- private static final int CONN_TIME_OUT = 20000; |
|
19 |
+ private static final int CONN_TIME_OUT = 10000; |
|
20 | 20 |
|
21 | 21 |
private static final String EQUAL_SIGN = "="; |
22 | 22 |
private static final String CONN_SIGN = "&"; |